﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NuGet.Protocol.Core.Types;
using QuickGet.Core.Services;

namespace QuickGet.Core
{
    public static class BuilderExtensions
    {
        public static IServiceCollection AddNugetServices(this IServiceCollection services)
        {
            services.AddScoped<NuGet.Common.ILogger>(sp => NuGet.Common.NullLogger.Instance);
            services.AddScoped<NugetResources>();
            services.AddScoped<SearchPackages>();
            services.AddScoped<DownloadPackage>();
            services.AddScoped<GetPackageMetadata>();
            services.AddScoped<ListPackageVersions>();
            services.AddScoped<ReadPackage>();
            services.AddSingleton<SourceCacheContext>(new SourceCacheContext());
            services.AddScoped<INugetSourceRepositoryResolver, NugetSourceRepositoryResolver>();
            services.AddScoped<INugetResourceResolver, NugetResourceResolver>();
            services.AddScoped<IFileDownloadService, FileDownloadService>();

            services.AddMemoryCache();

            services.AddTransient<IConfigureOptions<QuickGetOptions>>(sp =>
            {
                var config = sp.GetService<IConfiguration?>();

                return new ConfigureOptions<QuickGetOptions>(options => config?.Bind("QuickGet", options));
            });

            return services;
        }
    }
}
